//
//  ViewController.swift
//  RequestLib
//
//  Created by meicam on 2020/10/9.
//  Copyright © 2020 刘东旭. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var textField: UITextField!
    @IBOutlet weak var searchClick: UIButton!
    @IBOutlet weak var tableView: UITableView!
//    var dataSource = [Info]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "SearchInfoCell")
        NotificationCenter.default.addObserver(self, selector: #selector(textChanged(notification:)), name: UITextField.textDidChangeNotification, object: nil)
        
    }
    
    @objc func textChanged(notification: Notification) {
        searchClick(searchClick!)
    }
    
    @IBAction func searchClick(_ sender: Any) {
        ApiRequest(urlString: "http://vip.hd181.com/api.php", method: .GET, parameters: ["wd":textField.text ?? ""], headers: nil, successCallBack: { obj in
            print("ok")
        }, failureCallBack: { err in
            print("err \(err)")
        }).start()
//        let req = LDXRequest(urlString: "http://vip.hd181.com/api.php", method: .GET, parameters: ["wd":textField.text ?? ""])
//        LDXNetwork.request(request: req, resultType: SearchInfo1.self) { ret, response, error in
//            let searchInfo: SearchInfo1 = ret!
//            self.dataSource = searchInfo .info
//            self.tableView.reloadData()
////            print(r)
//        }
//        let request = Request<SearchInfo>(host: "http://vip.hd181.com/", path: "api.php", method: .GET, parameters: ["wd":textField.text ?? ""], headers: nil, httpBody: nil)
//        request.request { (searchInfo) in
//            print(searchInfo?.title ?? "")
//            self.dataSource = searchInfo!.info
//            self.tableView.reloadData()
//        }
    }
    
}

extension ViewController: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//        return dataSource.count
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SearchInfoCell", for: indexPath)
//        let info = dataSource[indexPath.row]
//        cell.textLabel?.text = info.title.removingPercentEncoding ?? ""
//
        return cell
    }
    
    
}
